package cn.micai.core.controller.admin;

import cn.micai.common.controller.BaseController;
import cn.micai.common.encode.MD5Pwd;
import cn.micai.common.web.session.SessionProvider;
import cn.micai.core.bean.user.Admin;
import cn.micai.core.bean.user.Buyer;
import cn.micai.core.service.user.AdminService;
import cn.micai.core.web.Constants;
import com.octo.captcha.service.image.ImageCaptchaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * 管理员
 * 登录
 * 退出
 *
 * @author zhaoxinguo on 2017/8/4.
 */
@Controller
@RequestMapping(value = "/control")
public class LoginController extends BaseController{

    @Autowired
    private ImageCaptchaService imageCaptchaService;

    @Autowired
    private AdminService adminService;

    //登录
    @RequestMapping(value = "/pcLogin.do", method = RequestMethod.POST)
    public String pcLogin(Admin admin, String captcha, HttpServletRequest request, ModelMap model){
        //1:验证码是否为null
        if(StringUtils.isNotBlank(captcha)){
            //1.JSESSIONID
            //2.验证码
            if(imageCaptchaService.validateResponseForID(sessionProvider.getSessionId(request), captcha)){
                //3.用户是否为NUll
                if(null != admin && StringUtils.isNotBlank(admin.getUsername())){
                    //4:密码是否为NUll
                    if(StringUtils.isNotBlank(admin.getPassword())){
                        Admin a = adminService.getAdminByUsername(admin.getUsername());
                        if(null != a){
                            //6:密码是否正确 Md5
                            if(a.getPassword().equals(mD5Pwd.encode(admin.getPassword()))){
                                //把用户对象 放进Session
                                sessionProvider.setAttribute(request, Constants.ADMIN_SESSION, a);
                                return "redirect:/control/index.do";
                            }else{
                                model.addAttribute("error", "密码输入错误");
                            }
                        }else{
                            model.addAttribute("error", "用户名输入错误");
                        }
                    }else{
                        model.addAttribute("error", "请输入密码");
                    }
                }else{
                    model.addAttribute("error", "请输入用户名");
                }
            }else{
                model.addAttribute("error", "验证码输入错误");
            }
        }else{
            model.addAttribute("error", "请填写验证码");
        }
        return "login";
    }

    //退出
    @RequestMapping(value = "/logout.do")
    public String logout(HttpServletRequest request){
        sessionProvider.logout(request);
        return "redirect:/control/login.do";
    }
}
